package com.boful.test;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;

public class MencoderTest2 {

	static File mencoder = new File("/usr/bin/mencoder");
	static File src = new File("/home/ligson/workspace/convert/clip2.rmvb");
	static File dest = new File("/home/ligson/workspace/convert/clip2.mp4");
	static File ffmpeg = new File("/usr/bin/ffmpeg");
	static File mediaInfo = new File("/usr/bin/mediainfo");
	static int videoBitrate = -1;
	static int audioBitrate = -1;
	static int width = -1;
	static int height = -1;

	public static void main(String[] args) throws Exception{

		if (!mencoder.exists()) {
			throw new FileNotFoundException("mencoder not found!");
		}
		if (!src.exists()) {
			throw new FileNotFoundException("source file not found!");
		}
		if (dest.exists()) {
			dest.delete();
		}
		dest.createNewFile();

		StringBuffer cmd = new StringBuffer();
		cmd.append(mencoder.getAbsolutePath());
		cmd.append(" " + src.getAbsolutePath());
		File fileTmp = new File(src.getParentFile(), src.getName() + ".avi");
		cmd.append(" -o " + fileTmp.getAbsolutePath());
		cmd.append(" -oac mp3lame ");
		cmd.append(" -ovc xvid");
		if (videoBitrate > 0) {
			cmd.append(" -xvidencopts bitrate=" + videoBitrate);
		} else {
			cmd.append(" -xvidencopts bitrate=600");
		}

		if (audioBitrate > 0) {
			cmd.append(" -lameopts preset=" + audioBitrate);
		} else {
			cmd.append(" -lameopts preset=64");
		}

		cmd.append(" -of avi ");
		if (width <= 0) {
			width = -3;
		}
		if (height <= 0) {
			height = -3;
		}
		if (!(height <= 0 && width <= 0)) {
			cmd.append(" -vf scale=" + width + ":" + height + ",harddup");
		}
		System.out.println("命令行:" + cmd);
		Process process = Runtime.getRuntime().exec(cmd.toString());
		BufferedReader  reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
		String line = null;
		while((line=reader.readLine())!=null){
			System.out.println(line);
		}
		reader.close();
	}

}
